package com.cskaoyan.controller;

import com.cskaoyan.bean.*;
import com.cskaoyan.service.MallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("admin")
public class MallController {
    @Autowired
    MallService mallService;

    @RequestMapping("/region/list")
    public BaseRespVo regionlist( ) {
        List<Region> regions = mallService.queryRegions();
        return BaseRespVo.ok(regions);
    }

    @RequestMapping("/brand/list")
    public BaseRespVo brandlist(Integer id, String name, Integer page, Integer limit, String sort, String order) {
        BrandBean brandBean = mallService.queryBrands(id,name,page,limit,sort,order);
        return BaseRespVo.ok(brandBean);
    }
    @RequestMapping("/brand/delete")
    public BaseRespVo brandDelete(@RequestBody BrandBO brand) {
        mallService.brandDelete(brand);
        return BaseRespVo.ok();
    }

    @RequestMapping("/brand/update")
    public BaseRespVo brandUpdate(@RequestBody Brand brand) {
        Brand returnBrand=mallService.brandUpdate(brand);
        return BaseRespVo.ok(returnBrand);
    }

    @RequestMapping("/brand/create")
    public BaseRespVo brandCreate(@RequestBody Brand brand) {
        String desc = brand.getDesc();
        int floorPrice = brand.getFloorPrice();
        String name = brand.getName();
        String picUrl = brand.getPicUrl();
        BrandCreateVO brandCreateVO=mallService.brandCreate(desc, String.valueOf(floorPrice),name,picUrl);
        return BaseRespVo.ok(brandCreateVO);
    }


    @RequestMapping("/category/list")
    public BaseRespVo categoryList(){
        List<CategoryListVO> categoryList=mallService.categoryList();
        return BaseRespVo.ok(categoryList);
    }

    @RequestMapping("/category/l1")
    public BaseRespVo categoryListL1(){
        List<CategoryVO> categoryVOList=mallService.categoryListL1();
        return BaseRespVo.ok(categoryVOList);
    }

    @RequestMapping("/category/create")
    public BaseRespVo create(@RequestBody CategoryChildren categoryCreate){
        CategoryCreateVO categoryCreateVO=mallService.create(categoryCreate);
        return BaseRespVo.ok(categoryCreateVO);
    }

    @RequestMapping("/category/delete")
    public BaseRespVo delete(@RequestBody CategoryChildren categoryCreate){
        mallService.delete(categoryCreate);
        return BaseRespVo.ok();
    }

    @RequestMapping("/category/update")
    public BaseRespVo update(@RequestBody CategoryId categoryUpdate){
        mallService.update(categoryUpdate);
        return BaseRespVo.ok();
    }


    @RequestMapping("issue/list")
    public BaseRespVo issuelist(String question,Integer page, Integer limit, String sort, String order) {
        IssueBean issueBean = mallService.queryIssues(question,page, limit, sort, order);
        return BaseRespVo.ok(issueBean);
    }

    @RequestMapping("issue/create")
    public BaseRespVo create(@RequestBody Issue issue){
        mallService.create(issue);
        return BaseRespVo.ok();
    }

    @RequestMapping("issue/delete")
    public BaseRespVo issuedelete(@RequestBody Issue issue){
        mallService.delete(issue);
        return BaseRespVo.ok();
    }

    @RequestMapping("issue/update")
    public BaseRespVo issueupdate(@RequestBody Issue issue){
        mallService.update(issue);
        return BaseRespVo.ok();
    }

    @RequestMapping("keyword/list")
    public BaseRespVo keywordList(Integer page, Integer limit, String keyword, String url, String sort, String order){
        ListBean listBean = mallService.queryKeyword(page, limit, keyword, url, sort, order);
        return BaseRespVo.ok(listBean);
    }

    @RequestMapping("keyword/update")
    public BaseRespVo keywordUpdate(@RequestBody Keyword keyword){
        Keyword keyword1 = mallService.updateAndQueryKeyword(keyword);
        return BaseRespVo.ok(keyword1);
    }

    @RequestMapping("keyword/create")
    public BaseRespVo keywordCreate(@RequestBody Keyword keyword){
        Keyword keyword1 = mallService.createKeyword(keyword);
        return BaseRespVo.ok(keyword1);
    }

    @RequestMapping("keyword/delete")
    public BaseRespVo keywordDelete(@RequestBody Keyword keyword){
        mallService.deleteKeyword(keyword.getId());
        return BaseRespVo.ok();
    }

    @RequestMapping("order/list")
    public BaseRespVo orderList(Integer page, Integer limit,Short[] orderStatusArray,String sort, String order,Integer userId,String orderSn){
        ListBean listBean = mallService.orderList(page,limit,orderStatusArray,sort,order,userId,orderSn);
        return BaseRespVo.ok(listBean);
    }
    @RequestMapping("order/detail")
    public BaseRespVo orderDetail(Integer id){
        OrderDetailList orderDetailList = mallService.orderDetail(id);
        return BaseRespVo.ok(orderDetailList);
    }
    @RequestMapping("order/refund")
    public BaseRespVo orderRefund(@RequestBody HashMap<String,Integer> map){
        Integer orderId = map.get("orderId");
        Integer refundMoney = map.get("refundMoney");
        try{
            mallService.orderRefund(orderId,refundMoney);
        }catch (Exception e){
            return BaseRespVo.ok(null,"订单退款失败");
        }
        return BaseRespVo.ok(null,"订单退款成功");
    }

    @RequestMapping("order/ship")
    public BaseRespVo orderShip(@RequestBody HashMap<String, Object> map){
        try {
            mallService.updateOrderShipAndStatus(map);
        }catch (Exception e){
            return BaseRespVo.ok("订单发货失败");
        }
        return BaseRespVo.ok("订单发货成功");

    }

    @RequestMapping("order/reply")
    public BaseRespVo orderreply(@RequestBody Map<String,Object> map){
        mallService.updateCommentReply(map);
        return BaseRespVo.ok("回复成功");
    }

}
